package com.tbit.main.controller;

import cn.hutool.crypto.SecureUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.pojo.*;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 品牌配置
 *
 * @author LMY
 * @create 2022-08-23 11:14
 */
@RestController
@RequestMapping("/brandRemoteConfigController")
public class BrandRemoteConfigController {
    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountService accountService;

    /**
     * 获取品牌文明骑行配置
     *
     * @param token
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(@NotNull String token) {

        AccountUser accountUser = tokenService.getAccount(token);

        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId(), BrandRemoteConfigConstant.WMQX);

        if (brandRemoteConfig == null) {
            return JsonResult.succ(0);
        } else {
            return JsonResult.succ(new BmsConfig().getBmsConfig(brandRemoteConfig.getParamKV()));
        }
    }

    /**
     * 获取 Tbit 的品牌配置
     *
     * @param token 用户标识
     * @return 品牌配置
     */
    @RequestMapping("/getConfigTbit")
    public JsonResult<BrandConfigBO> getConfigTbit(@NotNull String token) {

        AccountUser accountUser = tokenService.getAccount(token);

        Account brand = accountService.getByAccountAndType(accountUser.getAccountId(), 1);
        BrandConfigBO brandTbitConfig = brandRemoteConfigService.getConfigTbit(brand.getAccountId());

        // appSecret md5 加密
        Optional.ofNullable(brandTbitConfig).ifPresent(brandConfig -> brandTbitConfig.setAppSecret(SecureUtil.md5(brandTbitConfig.getAppSecret())));

        return JsonResult.succ(brandTbitConfig);
    }


    /**
     * 获取品牌技术人员配置
     *
     * @param token
     * @return
     */
    @RequestMapping("/getByPhone")
    public JsonResult<List<AfterSaleContact>> getByPhone(@NotNull String token) {

        AccountUser accountUser = tokenService.getAccount(token);

        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId(), BrandRemoteConfigConstant.SALE);

        if (brandRemoteConfig == null) {
            return JsonResult.succ(new LinkedList<>());
        } else {
            return JsonResult.succ(AfterSaleContact.parseList(brandRemoteConfig.getParamKV()));
        }
    }

    /**
     * 根据品牌id查询密钥总开关
     *
     * @param brandId 品牌id
     */
    @GetMapping("/getOperaPswEnable")
    public JsonResult getOperaPswEnable(Integer brandId) {
        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(brandId, BrandRemoteConfigConstant.OPERA_PSW);
        if (brandRemoteConfig == null) {
            BrandRemoteConfig brandRemoteConfigDefault = new BrandRemoteConfig();
            brandRemoteConfigDefault.setAccountId(brandId);
            brandRemoteConfigDefault.setConfigType(BrandRemoteConfigConstant.OPERA_PSW);
            brandRemoteConfigDefault.setParamKV("1");
            brandRemoteConfigService.insert(brandRemoteConfigDefault);
            brandRemoteConfig = brandRemoteConfigDefault;
        }
        return JsonResult.succ(Objects.equals(brandRemoteConfig.getParamKV(), "1"));
    }

}
